<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter='15'>
          <el-col :lg="6" :md="12">
            <el-form-item label="搜索查询:">
              <el-input v-model="where.keyword" clearable placeholder="输入关键词"/>
            </el-form-item>
          </el-col>
            <el-col :lg="6" :md="12" v-if="permission.includes('sys:personalWarehouse:superior')">
            <el-form-item label="人员：">
              <el-select
                v-model="where.user_id"
                filterable
                remote
                clearable
                reserve-keyword
                placeholder="搜索人员名称"
                :remote-method="remoteMethod"
                :loading="loading">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.realname"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:bom:index')" class="ele-btn-icon" icon="el-icon-search"
                         type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where"
                     height="calc(100vh - 308px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">

        </template>

        <template slot="warn_status" slot-scope="{row}">
          <el-tag
            :type="row.safe_quantity>=row.quantity?'danger':'success'"
            effect="dark">
            {{ row.safe_quantity >= row.quantity ? '超限' : '正常' }}
          </el-tag>
        </template>

        <template slot="real_count" slot-scope="{row}">

          {{row.quantity-row.disabled_inventory}}
        </template>


        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link v-if="permission.includes('sys:bom-personalWarehouse:return')" :underline="false" type="primary" :disabled="row.disabled_inventory>=row.quantity"
            @click="returnProduct(row)"
          >
            退回
          </el-link>

        </template>
      </ele-pro-table>
    </el-card>
    <returnProduct  ref="returnProduct" @reload="reload"></returnProduct>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import returnProduct  from './components/return.vue'
export default {
  name: 'SystemLevel',
  components: {
    returnProduct
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      warehouse_name: '',
      flag: false,
      // 表格数据接口inventories
      url: '/user_inventories',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'number',
          label: '编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'name',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'type',
          label: '类别',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'part_category_model',
          label: '型号',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'part_category_uom',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'quantity',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          slot: 'real_count',
          label: '实际库存',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'disabled_inventory',
          label: '不可用量',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 100,
          align: 'center',
          resizable: false,
          slot: 'action',
          emptyText: '暂无',
          fixed: "right"
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      options: [],
      /*入库类别*/
      storage_types: [],
      /*出库类别*/
      out_types:[],
      /*人员加载*/
      loading:false,
    };
  },
  mounted() {
    this.getParams()
    this.remoteMethod('')
  },
  methods: {
    /*搜索人员*/
    remoteMethod(query) {
        this.loading = true;
        this.$http.get(`/user/index?limit=10&realname=${query} `).then((res) => {
          this.options=res.data.data.filter(item => {
            return item.id !== 1
          });
          this.loading = false;
        });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.warehouse_name = ''
      this.where = {};
      this.reload();
    },
    /*获取参数*/
    getParams() {
      this.$http.get(`/inventories/params`).then((res) => {
        if (res.data.code === 0) {
          this.storage_types = res.data.data.storage_types
          this.out_types = res.data.data.out_types
        }
      })
    },
    returnProduct(info){
      this.$refs.returnProduct.active(info)
    },
    /*选择仓库*/
    handleSelectStock(val) {
      this.where.warehouse_id = val.id
    },
    /*获取仓库列表*/
    querySearch(query, cb) {
      query = query ? query : ''
      this.$http.get(`/warehouses?name=${query} `).then((res) => {
        cb(res.data.data)
      });
    },
  }
}
</script>

<style scoped>
</style>
